using Config;
using Data;
using Data.Bag;
using Entity.Player;
using MoonFramework.Buff;
using MoonFramework.Scripts.Event;
using MoonFramework.Template;
using MoonFramework.Test;
using Tool;
using Tool.Name;
using UnityEngine;

namespace UI.Bag
{
    [UIElement(false, "UI/UI_ToolBarWindow", 1)]
    public class UI_ToolBarWindow : UI_BagWindow
    {
        private ToolBarData _toolBarData;
        [SerializeField] private UI_ItemSlot _weaponSlot; //武器槽

        #region 任务
        protected override void RegisterEvent()
        {
            base.RegisterEvent();
            EventCenter.Instance.Register(EventName.WeaponAttackSucceed);
        }

        protected override void SendEvent()
        {
            base.SendEvent();
            EventCenter.Instance.SendCommand(EventName.WeaponAttackSucceed, OnWeaponAttackSucceed);
        }

        protected override void CancelEventListener()
        {
            base.CancelEventListener();
            EventCenter.Instance.RemoveCommand(EventName.WeaponAttackSucceed, OnWeaponAttackSucceed);
        }
        
        private void OnWeaponAttackSucceed()
        {
            //防止已经破坏还在同时计算
            if(_toolBarData.WeaponSlotItemData == null) return; 
            
            ItemWeaponData weaponData = _toolBarData.WeaponSlotItemData.ItemTypeData as ItemWeaponData;
            ItemWeaponInfo weaponInfo = _toolBarData.WeaponSlotItemData.Config.ItemTypeInfo as ItemWeaponInfo;
            weaponData.Durability -= weaponInfo.AttackValue;
            if (weaponData.Durability <= 0)
            {
                //损坏
                _toolBarData.RemoveWeapon();
                //武器槽拿掉武器
                _weaponSlot.InitData(null);
                //通知玩家卸掉武器
                PlayerController.Instance.Player.WeaponController.ChangeWeapon(null, PlayerController.Instance.Player);
            }
            else
            {
                //ui更新耐久度
                _weaponSlot.UpdateCountTextView();
            }
        }
        #endregion

        public void InitData()
        {
            //初始化存档数据
            InitBagData();
            //初始化格子
            InitSlotData();
            //初始化玩家手中的武器
            PlayerController.Instance.Player.WeaponController.ChangeWeapon(_toolBarData.WeaponSlotItemData, PlayerController.Instance.Player);
        }

        private void InitBagData()
        {
            //确定存档数据
            _bagData = ArchiveManager.Instance.ToolBarData;
            _toolBarData = (ToolBarData)_bagData;
        }

        protected override void InitSlotData()
        {
            for (int i = 0; i < _bagData?.ItemDataList.Length; i++)
            {
                _slots[i].Init(i, this, UseItem);
                _slots[i].InitData(_bagData.ItemDataList[i]);
            }
            //给武器进行初始化
            UI_ItemSlot.WeaponSlot = _weaponSlot;
            _weaponSlot.Init(_toolBarData.ItemDataList.Length, this, UseItem);
            _weaponSlot.InitData(_toolBarData?.WeaponSlotItemData);
        }
        
        /// <summary>
        /// 移除一个物品，而且不考虑是否堆积
        /// </summary>
        public override void RemoveItem(int index)
        {
            //武器
            if (index == _bagData.ItemDataList.Length)
            {
                _toolBarData.RemoveWeapon();
                _weaponSlot.InitData(null);
                //武器被移除同步数据
                EventCenter.Instance.Callback<ItemData>(EventName.UpdateWeapon, null);
            }
            //普通物品
            else
            {
                _bagData.RemoveItem(index);
                _slots[index].InitData(null);
            }
        }

        public override void DiscardItem(int index)
        {
            if (index == _slots.Count)
            {
                RemoveItem(index);
                return;
            }
            base.DiscardItem(index);
        }

        public override void SetItem(int index, ItemData itemData)
        {
            if (index == _toolBarData.ItemDataList.Length)
            {
                _toolBarData.SetWeapon(itemData);
                _weaponSlot.InitData(itemData);
                
                //将武器数据同步给玩家
                EventCenter.Instance.Callback(EventName.UpdateWeapon, itemData);
                return;
            }
            base.SetItem(index, itemData);
        }

        private string UseItem(int index)
        {
            //TODO:可能有多重buff影响，待buff加入重构
            //暂时只有缴械会影响无法持有物品，眩晕会当场直接await
            if (PlayerController.Instance.CheckBuff(nameof(DisarmBuff))) return PlayerAudioName.Fail;
            //拿下武器
            if (index == _slots.Count)
            {
                int emptyIndex = GetEmptySlotIndex();
                if (emptyIndex >= 0)
                {
                    //  武器槽和空格子 进行 交换
                    UI_ItemSlot.SwapSlotItem(_weaponSlot, _slots[emptyIndex]);
                    return PlayerAudioName.TakeUpWeapon;
                }
                //说明没有空格子，直接放回
                return PlayerAudioName.Fail;
            }
            ItemData itemData = _slots[index].ItemData;
            switch (itemData.Config.ItemType)
            {
                case ItemType.Weapon:
                    // 装备武器
                    UI_ItemSlot.SwapSlotItem(_weaponSlot, _slots[index]);
                    return PlayerAudioName.TakeUpWeapon;
                case ItemType.Consumable:
                    ItemConsumableInfo info = itemData.Config.ItemTypeInfo as ItemConsumableInfo;
                    if(info.RecoverHp != 0) EventCenter.Instance.Callback(EventName.RecoverHp, info.RecoverHp);
                    if(info.RecoverHunger != 0) EventCenter.Instance.Callback(EventName.RecoverHunger, info.RecoverHunger);
                    //更新物品的数量
                    BaseUsableItemData usableItemData = itemData.ItemTypeData as BaseUsableItemData;
                    usableItemData.Count--;
                    if(usableItemData.Count <= 0) RemoveItem(index);
                    else _slots[index].UpdateCountTextView();
                    return PlayerAudioName.ConsumableOk;
                default:
                    return PlayerAudioName.Fail;
            }
        }
    }
}